<template>
    <el-card shadow="never" class="access">
        <ListHeader @create="handleCreate" @refresh="getData"></ListHeader>
        <el-tree :data="tableData" :props="{ label: 'name', children: 'child' }" v-loading="loading" node-key="id">
            <template #default="{ node, data }">
                <div class="accessTreeBody">
                    <span>{{ data.name }}</span>
                    <div class="accessTreeBodyText">
                        <el-button text type="primary" size="small" @click="openGoodsDrawer(data)" :loading="data.goodsDrawerLoading">推荐商品</el-button>
                        <el-switch :modelValue="data.status" :active-value="1" :inactive-value="0"
                            @change="handleStatusChange($event, data)" @click.native.stop>
                        </el-switch>
                        <el-button text type="primary" size="small" @click.stop="handleEdit(data)">修改</el-button>
                        <el-popconfirm title="是否删除该记录" confirmButtonText="确认" cancelButtonText="取消"
                            @confirm.stop="handleDelete(data.id)">
                            <template #reference>
                                <el-button text type="primary" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </div>
            </template>
        </el-tree>
        <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSubmit">
            <el-form :model="form" ref="formRef" :rules='rules' label-width="80px" :inline="false">
                <el-form-item label="分类名称" prop="name" placeholder="请输入名称">
                    <el-input v-model="form.name" style="width: 30%"></el-input>
                </el-form-item>
            </el-form>
        </FormDrawer>

        <GoodsDrawer ref="goodsDrawerRef"></GoodsDrawer>
    </el-card>
</template>
<script setup>
import ListHeader from '@/components/ListHeader.vue';
import { getCateGoryList, createCategory, updateCategory, updateCategoryStatus, deleteCategory } from '@/api/category'
import { useInitTable, useInitForm } from '@/hooks/useCommon'
import FormDrawer from '@/components/FormDrawer.vue'
import GoodsDrawer from './components/GoodsDrawer.vue'
import { ref } from 'vue'

const {
    loading,
    getData,
    tableData,
    handleDelete,
    handleStatusChange
} = useInitTable({
    getList: getCateGoryList,
    onGetListSuccess: (res) => {
        tableData.value = res.map(o => {
            o.goodsDrawerLoading = false
            return o
        })
    },
    delete: deleteCategory,
    updateStatus: updateCategoryStatus
})

// 新增、修改
const {
    formDrawerRef,
    formRef,
    rules,
    form,
    drawerTitle,
    handleSubmit,
    handleCreate,
    handleEdit
} = useInitForm({
    form: {
        name: ""
    },
    rules: {
    },
    getData,
    update: updateCategory,
    create: createCategory
})

const goodsDrawerRef = ref(null)
const openGoodsDrawer = (data) => goodsDrawerRef.value.open(data)
</script>
<style scoped lang="less">
.access {
    border: 0;

    :deep(.el-tree-node__content) {
        padding: 20px 0;
    }

    .accessTreeBody {
        flex: 1;
        display: flex;
        align-items: center;
        font-style: 14px;
        padding-right: 8px;

        .accessIcon {
            margin-left: 2px;
        }

        .accessTreeBodyText {
            margin-left: auto;
        }
    }
}
</style> 